/**
 * 
 */
package com.estock.web.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.estock.common.consts.DaoQueryConsts;
import com.estock.common.dao.impl.BaseDaoImpl;
import com.estock.common.model.security.Stock;
import com.estock.common.model.security.StockCategory;
import com.estock.common.util.StringUtil;
import com.estock.common.util.support.ESLogger;
import com.estock.web.dao.StockCategoryDao;
import com.estock.web.dao.StockDao;

/**
 * @author richard
 *
 */
@Repository
public class StockCategoryDaoImpl extends BaseDaoImpl<StockCategory> implements StockCategoryDao {
	
	private static final ESLogger logger = ESLogger.getLogger(StockCategoryDaoImpl.class);
	
	@Resource
	private StockDao stockDao;
	
	public StockDao getStockDao() {
		return stockDao;
	}
	
	@Override
	public StockCategory findStockCategoryByName(String categoryName) {
		logger.info("Start find stock category by name %s", categoryName);
		Criteria criteria = getSession().createCriteria(StockCategory.class);
		criteria.add(Restrictions.eq(DaoQueryConsts.STOCKCATEGORY_NAME, categoryName));
		List<StockCategory> results = findByCriteria(criteria);
		
        if (StringUtil.isEmpty(results)) return null;
        // fix LazyInitializationException error
//        for (Stock s: results.get(0).getStocks()) {
//			s.getName();
//		}
        
        return results.get(0);
	}

	@Override
	public List<StockCategory> findCategoriesByStockId(int stockId) {
		logger.info("Start find stock categories by stock id %d", stockId);

//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put(DaoQueryConsts.STOCKCATEGORY_STOCKID, stockid);
//		try {
//			return findByNamedQuery(DaoQueryConsts.getCategoriesByStockId, params);
//		} catch (Exception e) {
//			throw new ECRuntimeException("can not get categories by stock id.", e);
//		}
		
		Stock stock = getStockDao().findById(Long.valueOf(stockId));
		List<StockCategory> categories = stock.getCategories();
		if (categories == null) return new ArrayList<StockCategory>();
		
		// fix LazyInitializationException error
//		for (StockCategory sc: categories) {
//			sc.getName();
//		}
		logger.info("get categories for stockId %d with size of %d", stockId, categories.size());
		return categories;
	}

	@Override
	public List<StockCategory> findAllActiveStockCategoies() {
		logger.info("Start finding all active stock categories");
		Criteria criteria = getSession().createCriteria(StockCategory.class);
		criteria.add(Restrictions.eq(DaoQueryConsts.STOCKCATEGORY_ISACTIVE, 1));
		criteria.addOrder(Order.asc(DaoQueryConsts.ID));
		List<StockCategory> results = findByCriteria(criteria);
		
        if (StringUtil.isEmpty(results)) return null;
        return results;
	}

	@Override
	public List<StockCategory> findCategoriesByStockCode(String code) {
		Stock stock = getStockDao().findStockByCode(code);
		
		return findCategoriesByStockId(stock.getId().intValue());
	}

}
